package com.ningxia.water.service.impl;

import com.ningxia.water.entity.TsApplicantPlan;
import com.ningxia.water.mapper.*;
import com.ningxia.water.service.ITsApplicantPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ningxia.water.utils.NumberUtils;
import com.ningxia.water.vo.out.ApplicantAndWellsVo;
import com.ningxia.water.vo.out.ChartVo;
import com.ningxia.water.vo.out.PlanVolumeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hz
 * @since 2021-03-15
 */
@Service
public class TsApplicantPlanServiceImpl extends ServiceImpl<TsApplicantPlanMapper, TsApplicantPlan> implements ITsApplicantPlanService {

    @Autowired
    private TsApplicantPlanMapper applicantPlanMapper;

    @Autowired
    private TsApplicantInfoMapper applicantInfoMapper;

    @Autowired
    private TsLicenseInfoMapper licenseInfoMapper;

    @Autowired
    private TsWellInfoMapper wellInfoMapper;

    @Override
    public ChartVo getAllVolume(String year) {
        // 年许可水量
        Double licenseVolume = licenseInfoMapper.getLicenseVolume();
        // 年计划水量
        Double planVolume = applicantPlanMapper.getPlanVolume(year);
        // 年累计水量
        String startTime = year + "-01";
        String endTime = year + "-12";
        Double cumulativeVolume = wellInfoMapper.getTotalConsumed(startTime, endTime);
        // 剩余计划水量
        if (planVolume == null){
            return new ChartVo(licenseVolume,0.0,0.0,0.0,0.0);
        }else if (cumulativeVolume == null){
            return new ChartVo(licenseVolume,planVolume,0.0,0.0,0.0);
        }
        Double remainPlanVolume = planVolume - cumulativeVolume;
        // 剩余许可水量
        Double remainLicenseVolume = licenseVolume - planVolume;
//        if (licenseVolume != null && planVolume != null && cumulativeVolume != null && remainPlanVolume != null && remainLicenseVolume != null) {
//            licenseVolume = Double.parseDouble(NumberUtils.getInstance().format(new BigDecimal(licenseVolume)));
//            planVolume = Double.parseDouble(NumberUtils.getInstance().format(new BigDecimal(planVolume)));
//            cumulativeVolume = Double.parseDouble(NumberUtils.getInstance().format(new BigDecimal(cumulativeVolume)));
//            remainPlanVolume = Double.parseDouble(NumberUtils.getInstance().format(new BigDecimal(remainPlanVolume)));
//            remainLicenseVolume = Double.parseDouble(NumberUtils.getInstance().format(new BigDecimal(remainLicenseVolume)));
//
//        }

        return new ChartVo(licenseVolume, planVolume, cumulativeVolume, remainPlanVolume, remainLicenseVolume);
    }

    @Override
    public List<ApplicantAndWellsVo> findAllWells() {
        return applicantInfoMapper.findAllWells();
    }

    @Override
    public List<ApplicantAndWellsVo> findAllWarnWells() {
        List<ApplicantAndWellsVo> list = applicantInfoMapper.findAllWarnWells();
        ListIterator<ApplicantAndWellsVo> it = list.listIterator();
        List<ApplicantAndWellsVo> resultList = new ArrayList<>();
        while (it.hasNext()) {
            ApplicantAndWellsVo aw = it.next();
            if (!aw.getWellNames().isEmpty())
                resultList.add(aw);
        }

        return resultList;
    }

    @Override
    public List<PlanVolumeVo> getPlanVolume(String applicantName) {
        // 获取当前时间
        LocalDateTime dateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy");
        String currentYear = dateTime.format(formatter);
        return applicantPlanMapper.getPlanVolumeInfos(applicantName, currentYear);
    }

    @Override
    public PlanVolumeVo getPlanVolumeByYear(Integer applicantId, String planYear) {

        return applicantPlanMapper.getPlanVolumeByYear(applicantId, planYear);
    }
}
